<template>
  <div style="position: relative; width: 100%; height: 100%; padding: 0">
    <div class="page-content" v-if="!showDetail && !showPatientInfo && !showPatientInfoTwo">
      <a-card style="width: 100%; height: 100%">
        <div class="header">
          <template v-if="enterpriseShow">
            <Condition
              :condition="condition"
              :btns="['search', 'reset']"
              @search="searchHandle"
              @reset="reset"
            />
          </template>
          <template v-else>
            <Condition
              :condition="conditionEnterprise"
              :btns="['search', 'reset']"
              @search="searchHandle"
              @reset="reset"
            />
          </template>
          <div class="tabEnterprise">
            <p
              :class="tabEnterpriseDataActive == index ? 'on' : ''"
              v-for="(item, index) in tabEnterpriseData"
              :key="index"
              @click="tabEnterpriseDataCli(item, index)"
            >
              {{ item.name }}
            </p>
          </div>
          <div style="display: flex; align-items: center; justify-content: space-between">
            <a-radio-group
              button-style="solid"
              v-model:value="raidoValue"
              @change="raidoValueChange"
            >
              <a-radio-button value="todo">
                {{ tabEnterpriseDataActive == 0 ? '待办' : '待执行' }}({{
                  dispatchAllCounts ? dispatchAllCounts.noSend : 0
                }})
              </a-radio-button>
              <a-radio-button value="done">
                {{ tabEnterpriseDataActive == 0 ? '已推送' : '已推送' }}({{
                  dispatchAllCounts ? dispatchAllCounts.send : 0
                }})
              </a-radio-button>
            </a-radio-group>
            <div>
              <a-button
                v-if="raidoValue == 'todo' && enterpriseShowThree == true"
                type="primary"
                @click="batchPush"
                >批量推送方案</a-button
              >
            </div>
          </div>
          <!-- <span> 共筛选出 {{ pagination.total }} 条数据 </span> -->
        </div>
        <div class="main">
          <a-table
            class="table"
            size="small"
            :loading="loading"
            :dataSource="List"
            :columns="columns"
            :pagination="pagination"
            @change="paginationChange"
            :sticky="{ offsetHeader: 0 }"
            :row-key="'id'"
            :row-selection="enterpriseShowTwo ? rowSelection : undefined"
          >
            <!-- :row-selection="rowSelection" -->
            <!-- <template #bodyCell="{ column, text, record, index }"> -->
            <template #bodyCell="{ column, record, index }">
              <template v-if="column.dataIndex == 'no'">
                {{ (pagination.current - 1) * pagination.pageSize + index + 1 }}
              </template>
              <template v-else-if="column.dataIndex === 'patName'">
                <span class="patName" @click="toDetail(record)" type="link">
                  {{ record.patName }} {{ record.gender }} {{ record.age }}
                </span>
              </template>
              <template v-else-if="column.dataIndex === 'todoNum'">
                <span class="patName" @click="toEnterprise(record)" type="link">
                  {{ record.todoNum }}
                </span>
              </template>
              <template v-else-if="column.dataIndex === 'doneNum'">
                <span class="patName" @click="toEnterprise(record)" type="link">
                  {{ record.doneNum }}
                </span>
              </template>

              <template v-else-if="column.dataIndex === 'memberState'">
                <a-tag
                  v-if="record.memberState == '生效中'"
                  style="color: #00b42a; background-color: #e8f7ff; border: 1px solid #00b42a"
                >
                  生效中
                </a-tag>
                <a-tag
                  v-if="record.memberState == '已中止'"
                  style="color: #1d2129; background-color: #f2f3f5; border: 1px solid #1d2129"
                >
                  已中止
                </a-tag>
                <a-tag
                  v-if="record.memberState == '已过期'"
                  style="color: #1d2129; background-color: #f2f3f5; border: 1px solid #1d2129"
                >
                  已过期
                </a-tag>
              </template>
              <template v-else-if="column.dataIndex === 'vipStatus'">
                <a-tag
                  v-if="record.vipStatus == '生效中'"
                  style="color: #00b42a; background-color: #e8f7ff; border: 1px solid #00b42a"
                >
                  生效中
                </a-tag>
                <!-- <a-tag
                  v-if="record.memberState == '已中止'"
                  style="color: #1d2129; background-color: #f2f3f5; border: 1px solid #1d2129"
                >
                  已中止
                </a-tag> -->
                <a-tag
                  v-if="record.vipStatus == '已过期'"
                  style="color: #1d2129; background-color: #f2f3f5; border: 1px solid #1d2129"
                >
                  已过期
                </a-tag>
              </template>
              <template v-else-if="column.dataIndex == 'buyTime'">
                <span v-if="record.buyTime">{{
                  dayjs(record.buyTime).format('YYYY-MM-DD HH:mm')
                }}</span>
                ~
                <span v-if="record.expiredTime">{{
                  dayjs(record.expiredTime).format('YYYY-MM-DD HH:mm')
                }}</span>
              </template>
              <template v-else-if="column.dataIndex == 'vipStartTime'">
                <span v-if="record.vipStartTime">{{
                  dayjs(record.vipStartTime).format('YYYY-MM-DD HH:mm')
                }}</span>
                ~
                <span v-if="record.vipEndTime">{{
                  dayjs(record.vipEndTime).format('YYYY-MM-DD HH:mm')
                }}</span>
              </template>
              <template v-else-if="column.dataIndex === 'questionnaireRecordId'">
                <a-button
                  type="link"
                  :disabled="!record.questionnaireRecordId"
                  @click="questionAnswerForm(record)"
                  >检后问卷</a-button
                >
              </template>
              <!-- <template v-else-if="column.dataIndex === 'questionState'">
                <span style="color: #00b42a" v-if="record.questionState">
                  <CheckCircleFilled two-tone-color="#00B42A" />
                  已填写
                </span>
                <span style="color: #f53f3f" v-if="!record.questionState">
                  <ExclamationCircleFilled two-tone-color="#F53F3F" />
                  未填写
                </span>
              </template> -->
              <template v-else-if="column.dataIndex === 'peOrgType'">
                <span
                  :class="[
                    'peOrgType',
                    record.peOrgType == 0
                      ? 'peOrgType0'
                      : record.peOrgType == 1
                        ? 'peOrgType1'
                        : record.peOrgType == 2
                          ? 'peOrgType2'
                          : 'peOrgType3'
                  ]"
                >
                  {{
                    record.peOrgType == 0
                      ? '独立'
                      : record.peOrgType == 1
                        ? '一级'
                        : record.peOrgType == 2
                          ? '二级'
                          : '三级'
                  }}
                </span>
              </template>
              <template v-else-if="column.dataIndex === 'pathName'">
                {{ record.pathName ? record.pathName : '/' }}
              </template>
              <template v-else-if="column.dataIndex === 'aiState'">
                <span style="color: #3491fa" v-if="record.aiState == '生成中'">
                  <ExclamationCircleFilled />
                  生成中
                </span>
                <span style="color: #f53f3f" v-else-if="record.aiState == '生成失败'">
                  <CloseCircleOutlined />
                  生成失败
                </span>
                <span style="color: #00b42a" v-else-if="record.aiState == '已生成'">
                  <CheckCircleFilled />
                  已生成
                </span>
                <span v-else>/</span>
              </template>
              <template v-else-if="column.dataIndex === 'serviceState'">
                <span style="color: #ff7d00" v-if="record.serviceState == 'TODO'">
                  <ExclamationCircleFilled two-tone-color="#FF7D00" />
                  待执行
                </span>
                <span style="color: #3491fa" v-else-if="record.serviceState == 'DOING'">
                  <ExclamationCircleFilled two-tone-color="#3491FA" />
                  处理中
                </span>
                <span style="color: #3491fa" v-else-if="record.serviceState == 'REPROCESSING'">
                  <ExclamationCircleFilled two-tone-color="#3491FA" />
                  处理中
                </span>
                <span style="color: #0fc6c2" v-else-if="record.serviceState == 'DONE'">
                  <CheckCircleFilled two-tone-color="#0FC6C2" />
                  已推送
                </span>
                <span style="color: #1d2129" v-else-if="record.serviceState == 'EXPIRED'">
                  <CloseCircleOutlined two-tone-color="#1d2129" />
                  已过期
                </span>
              </template>
              <template v-else-if="column.dataIndex === 'action'">
                <a-button
                  v-if="raidoValue === 'todo' && tabEnterpriseDataActive == 0"
                  type="link"
                  @click="addOrUpdateHandle(record)"
                  :disabled="record.memberState != '生效中'"
                >
                  操作
                </a-button>
                <a-button
                  v-else-if="raidoValue === 'done' && tabEnterpriseDataActive == 0"
                  type="link"
                  @click="addOrUpdateHandle(record)"
                  :disabled="record.memberState != '生效中'"
                  >修改</a-button
                >
                <a-button v-else type="link" @click="toEnterprise(record)">查看员工</a-button>
              </template>
            </template>
          </a-table>
        </div>
      </a-card>
    </div>
    <FollowInfo ref="followInfoRef" />
    <ManaPlan ref="manaPlanRef" v-if="showDetail" @close="goBack" />
    <PatientInfo ref="patientInfoRef" v-if="showPatientInfo" @close="gopatientBack" />
    <ImplementEnterprise
      ref="patientInfoRefTwo"
      v-if="showPatientInfoTwo"
      @closed="gopatientBackTwo"
    />
    <!-- 推送方案确认框 -->
    <a-modal
      v-model:open="isPushPlan"
      title=""
      :closable="false"
      :footer="null"
      :maskClosable="false"
    >
      <a-spin :spinning="isCreateHealthInfo">
        <div style="padding-top: 16px">
          <a-empty :image-style="{ height: '110px' }">
            <template #image>
              <div>
                <img
                  style="width: 162px; height: 110px"
                  src="@/assets/health/plan-image.png"
                  alt=""
                />
              </div>
            </template>
            <template #description>
              <div style="margin: 24px 0; font-size: 16px; color: #1d2129; text-align: left">
                点击确认向已勾选的患者批量推送方案，健康任务将在明天开始正式提醒，确定向患者推送方案吗？
              </div>
              <div>
                <a-space>
                  <a-button @click="isPushPlan = false">暂不推送</a-button>
                  <a-button type="primary" @click="pushPlanBtn">推送方案</a-button>
                </a-space>
              </div>
            </template>
          </a-empty>
        </div>
      </a-spin>
    </a-modal>
  </div>
</template>
<script lang="ts" setup>
import Condition from '@/components/ConditionSearch/index.vue'
import {
  CheckCircleFilled,
  ExclamationCircleFilled,
  CloseCircleOutlined
} from '@ant-design/icons-vue'
import { ref, onMounted, reactive, unref } from 'vue'
import ManaPlan from './manaPlan.vue'
import { getDispatchList, batchPushScheme, managerPage } from '@/api/healthManaWorkbench/index'

import PatientInfo from '@/views/examinatePatients/patientInfo.vue'
import dayjs from 'dayjs'
import { useRoute, useRouter } from 'vue-router'
import store from '@/store'
import { message } from 'ant-design-vue'
import ImplementEnterprise from './ImplementEnterprise.vue'
import FollowInfo from '@/views/specializedDiseaseWorkbench/dispatch/followInfo.vue'

const route = useRoute()
const router = useRouter()
const loading = ref<boolean>(false)
const List = ref<any[]>([])
const showDetail = ref(false)
const showPatientInfo = ref(false)
const raidoValue = ref('todo')
const showPatientInfoTwo = ref(false)
type Key = string | number
const selectedKeys = ref<Key[]>([])
const isPushPlan = ref(false)
const isCreateHealthInfo = ref(false)
const tabEnterpriseDataActive = ref(0)
const tabEnterpriseData: any = ref([
  {
    name: '个人',
    value: '1'
  },
  {
    name: '团队',
    value: '2'
  }
])
const conditionEnterprise = ref([])
const conditionTodoTwo = ref([
  {
    title: '',
    dataIndex: 'peOrgName',
    type: 'input',
    value: '',
    placeholder: '请输入企业名称/ID'
  },
  {
    title: '企业类型',
    dataIndex: 'peOrgType',
    type: 'select',
    data: [
      {
        label: '独立',
        value: '0'
      },
      {
        label: '一级',
        value: '1'
      },
      {
        label: '二级',
        value: '2'
      },
      {
        label: '三级',
        value: '3'
      }
    ],
    value: undefined,
    placeholder: '请选择企业类型'
  },
  {
    title: '会员状态',
    dataIndex: 'vipStatus',
    type: 'select',
    value: undefined,
    placeholder: '请选择会员状态',
    data: [
      {
        label: '生效中',
        value: 2
      },
      {
        label: '已过期',
        value: 9
      }
    ]
  },
  {
    title: '会员购买时间',
    dataIndex: 'buyTime',
    type: 'dayRange',
    value: [],
    format: 'YYYY-MM-DD HH:mm',
    showTime: true,
    style: { width: '300px' }
  }
])
const conditionDoneTwo = ref([
  {
    title: '',
    dataIndex: 'peOrgName',
    type: 'input',
    value: '',
    placeholder: '请输入企业名称/ID'
  },
  {
    title: '企业类型',
    dataIndex: 'peOrgType',
    type: 'select',
    data: [
      {
        label: '独立',
        value: '0'
      },
      {
        label: '一级',
        value: '1'
      },
      {
        label: '二级',
        value: '2'
      },
      {
        label: '三级',
        value: '3'
      }
    ],
    value: undefined,
    placeholder: '请选择企业类型'
  },
  {
    title: '会员状态',
    dataIndex: 'memberState',
    type: 'select',
    value: undefined,
    data: [
      {
        label: '生效中',
        value: 2
      },
      {
        label: '已过期',
        value: 9
      }
    ]
  },
  {
    title: '会员购买时间',
    dataIndex: 'buyTime',
    type: 'dayRange',
    value: [],
    format: 'YYYY-MM-DD HH:mm',
    showTime: true,
    style: { width: '300px' }
  }
])
const columnsTodoTwo = ref<any[]>([
  {
    title: '序号',
    dataIndex: 'no',
    key: 'no',
    align: 'left',
    width: 80
  },
  {
    title: '企业ID',
    dataIndex: 'peOrgId',
    key: 'peOrgId',
    align: 'left'
  },
  {
    title: '企业名称',
    dataIndex: 'peOrgName',
    key: 'peOrgName',
    align: 'left'
  },
  {
    title: '企业类型',
    dataIndex: 'peOrgType',
    key: 'peOrgType',
    align: 'left'
  },
  {
    title: '会员状态',
    dataIndex: 'vipStatus',
    key: 'vipStatus',
    align: 'left'
  },
  {
    title: '会员有效期',
    dataIndex: 'vipStartTime',
    key: 'vipStartTime',
    align: 'left',
    width: 150
  },
  {
    title: '会员套餐',
    dataIndex: 'pePackageName',
    key: 'pePackageName',
    align: 'left'
  },
  // {
  //   title: '创建时间',
  //   dataIndex: 'createTime',
  //   key: 'createTime',
  //   align: 'left'
  // },
  {
    title: '待执行员工',
    dataIndex: 'todoNum',
    key: 'todoNum',
    align: 'left'
  },
  {
    title: '操作',
    dataIndex: 'action',
    key: 'action',
    align: 'left',
    width: 100
  }
])
const columnsDoneTwo = ref<any[]>([
  {
    title: '序号',
    dataIndex: 'no',
    key: 'no',
    align: 'left',
    width: 80
  },
  {
    title: '企业ID',
    dataIndex: 'peOrgId',
    key: 'peOrgId',
    align: 'left'
  },
  {
    title: '企业名称',
    dataIndex: 'peOrgName',
    key: 'peOrgName',
    align: 'left'
  },
  {
    title: '企业类型',
    dataIndex: 'peOrgType',
    key: 'peOrgType',
    align: 'left'
  },
  {
    title: '会员状态',
    dataIndex: 'memberState',
    key: 'memberState',
    align: 'left'
  },
  {
    title: '会员有效期',
    dataIndex: 'vipStartTime',
    key: 'vipStartTime',
    align: 'left',
    width: 150
  },
  {
    title: '会员套餐',
    dataIndex: 'pePackageName',
    key: 'pePackageName',
    align: 'left'
  },
  // {
  //   title: '创建时间',
  //   dataIndex: 'createTime',
  //   key: 'createTime',
  //   align: 'left'
  // },

  {
    title: '服务团队',
    dataIndex: 'groupName',
    key: 'groupName',
    align: 'left'
  },
  {
    title: '服务组长',
    dataIndex: 'groupManagerName',
    key: 'groupManagerName',
    align: 'left'
  },
  {
    title: '操作',
    dataIndex: 'action',
    key: 'action',
    align: 'left',
    width: 100
  }
])
const enterpriseShow = ref(true)
const enterpriseShowTwo = ref(true)
const enterpriseShowThree = ref(true)

const dispatchAllCounts = ref()
const condition: any = ref([])
const conditioning = ref<any[]>([
  {
    title: '会员购买时间',
    dataIndex: 'buyTime',
    type: 'dayRange',
    value: [],
    format: 'YYYY-MM-DD HH:mm',
    showTime: true,
    style: { width: '300px' }
  },
  {
    title: '报告生成时间',
    dataIndex: 'visitTime',
    type: 'dayRange',
    value: [],
    format: 'YYYY-MM-DD HH:mm',
    showTime: true,
    style: { width: '300px' }
  },
  {
    title: '',
    dataIndex: 'condition',
    type: 'input',
    value: '',
    placeholder: '请输入姓名/证件号'
  },
  {
    title: '',
    dataIndex: 'questionState',
    type: 'select',
    value: undefined,
    data: [
      {
        label: '未填写',
        value: false
      },
      {
        label: '已填写',
        value: true
      }
    ],
    placeholder: '请选择问卷状态'
  },
  {
    title: '服务状态',
    dataIndex: 'serviceState',
    type: 'select',
    value: undefined,
    data: [
      {
        label: '待执行',
        value: 'TODO'
      },
      {
        label: '处理中',
        value: 'DOING'
      }
    ],
    placeholder: '请选择服务状态'
  },
  {
    title: '会员状态',
    dataIndex: 'memberState',
    type: 'select',
    value: undefined,
    placeholder: '请选择会员状态',
    data: [
      {
        label: '生效中',
        value: 1
      },
      {
        label: '已中止',
        value: 4
      },
      {
        label: '已过期',
        value: 8
      }
    ]
  }
])
const conditionTodo = ref<any[]>([
  {
    title: '会员购买时间',
    dataIndex: 'buyTime',
    type: 'dayRange',
    value: [],
    format: 'YYYY-MM-DD HH:mm',
    showTime: true,
    style: { width: '300px' }
  },
  {
    title: '报告生成时间',
    dataIndex: 'visitTime',
    type: 'dayRange',
    value: [],
    format: 'YYYY-MM-DD HH:mm',
    showTime: true,
    style: { width: '300px' }
  },
  {
    title: '',
    dataIndex: 'condition',
    type: 'input',
    value: '',
    placeholder: '请输入姓名/证件号'
  },
  {
    title: '',
    dataIndex: 'questionState',
    type: 'select',
    value: undefined,
    data: [
      {
        label: '未填写',
        value: false
      },
      {
        label: '已填写',
        value: true
      }
    ],
    placeholder: '请选择问卷状态'
  },
  {
    title: '会员状态',
    dataIndex: 'memberState',
    type: 'select',
    value: undefined,
    placeholder: '请选择会员状态',
    data: [
      {
        label: '生效中',
        value: 1
      },
      {
        label: '已中止',
        value: 4
      },
      {
        label: '已过期',
        value: 8
      }
    ]
  }
])
const columns: any = ref([])
const conditionEd = ref<any[]>([
  {
    title: '会员购买时间',
    dataIndex: 'buyTime',
    type: 'dayRange',
    value: [],
    format: 'YYYY-MM-DD HH:mm',
    showTime: true,
    style: { width: '300px' }
  },
  {
    title: '报告生成时间',
    dataIndex: 'visitTime',
    type: 'dayRange',
    value: [],
    format: 'YYYY-MM-DD HH:mm',
    showTime: true,
    style: { width: '300px' }
  },
  {
    title: '',
    dataIndex: 'condition',
    type: 'input',
    value: '',
    placeholder: '请输入姓名/证件号'
  },
  {
    title: '',
    dataIndex: 'questionState',
    type: 'select',
    value: undefined,
    data: [
      {
        label: '未填写',
        value: false
      },
      {
        label: '已填写',
        value: true
      }
    ],
    placeholder: '请选择问卷状态'
  }
])
const columnsTodo = ref<any[]>([
  {
    title: '序号',
    dataIndex: 'no',
    key: 'no',
    align: 'left',
    width: 80
  },
  {
    title: '账号',
    dataIndex: 'userPhone',
    key: 'userPhone',
    align: 'left'
  },
  {
    title: '患者手机号',
    dataIndex: 'phone',
    key: 'phone',
    align: 'left'
  },
  {
    title: '患者',
    dataIndex: 'patName',
    key: 'patName',
    align: 'left'
  },
  //   {
  //     title: '性别',
  //     dataIndex: 'gender',
  //     key: 'gender',
  //     align: 'left'
  //   },
  //   {
  //     title: '年龄',
  //     dataIndex: 'age',
  //     key: 'age',
  //     align: 'left'
  //   },
  {
    title: '证件号',
    dataIndex: 'idNo',
    key: 'idNo',
    align: 'left'
  },
  {
    title: '会员状态',
    dataIndex: 'memberState',
    key: 'memberState',
    align: 'left'
  },
  {
    title: '会员有效期',
    dataIndex: 'buyTime',
    key: 'buyTime',
    align: 'left',
    width: 150
  },
  {
    title: '报告生成时间',
    dataIndex: 'visitTime',
    key: 'visitTime',
    align: 'left'
  },
  {
    title: '管理路径',
    dataIndex: 'pathName',
    key: 'pathName',
    align: 'left'
  },
  {
    title: '问卷',
    dataIndex: 'questionnaireRecordId',
    key: 'questionnaireRecordId',
    align: 'left',
    width: 100
  },
  // {
  //   title: '问卷状态',
  //   dataIndex: 'questionState',
  //   key: 'questionState',
  //   align: 'left'
  // },
  {
    title: 'AI状态',
    dataIndex: 'aiState',
    key: 'aiState',
    align: 'left'
  },
  {
    title: '服务状态',
    dataIndex: 'serviceState',
    key: 'serviceState',
    align: 'left'
  },
  {
    title: '操作',
    dataIndex: 'action',
    key: 'action',
    align: 'left',
    width: 100
  }
])
const columnsDone = ref<any[]>([
  {
    title: '序号',
    dataIndex: 'no',
    key: 'no',
    align: 'left',
    width: 80
  },
  {
    title: '账号',
    dataIndex: 'userPhone',
    key: 'userPhone',
    align: 'left'
  },
  {
    title: '患者手机号',
    dataIndex: 'phone',
    key: 'phone',
    align: 'left'
  },
  {
    title: '患者',
    dataIndex: 'patName',
    key: 'patName',
    align: 'left'
  },
  {
    title: '证件号',
    dataIndex: 'idNo',
    key: 'idNo',
    align: 'left'
  },
  {
    title: '会员状态',
    dataIndex: 'memberState',
    key: 'memberState',
    align: 'left'
  },
  {
    title: '会员有效期',
    dataIndex: 'buyTime',
    key: 'buyTime',
    align: 'left',
    width: 150
  },
  {
    title: '报告生成时间',
    dataIndex: 'visitTime',
    key: 'visitTime',
    align: 'left'
  },
  {
    title: '管理路径',
    dataIndex: 'pathName',
    key: 'pathName',
    align: 'left'
  },
  {
    title: '问卷',
    dataIndex: 'questionnaireRecordId',
    key: 'questionnaireRecordId',
    align: 'left',
    width: 100
  },
  // {
  //   title: '问卷状态',
  //   dataIndex: 'questionState',
  //   key: 'questionState',
  //   align: 'left'
  // },
  {
    title: '服务状态',
    dataIndex: 'serviceState',
    key: 'serviceState',
    align: 'left'
  },
  {
    title: '操作',
    dataIndex: 'action',
    key: 'action',
    align: 'left',
    width: 100
  }
])
const zhuguanShow = ref(false)
const zuzhangShow = ref(false)
const pagination = ref<any>({
  current: 1,
  total: 0,
  pageSize: 10,
  showSizeChanger: true,
  pageSizeOptions: ['10', '20', '50', '100'],
  showQuickJumper: true
})
const paginationChange = (page: any) => {
  pagination.value = { ...page }
  loading.value = true
  getDataList()
}

const getDataList = () => {
  List.value = []
  let obj = {}
  if (tabEnterpriseDataActive.value == 0) {
    obj = condition.value.reduce((c, v) => {
      if (v.value || v.value === 0 || v.value === false) {
        if (v.dataIndex == 'visitTime') {
          return Object.assign(c, {
            visitTimeSta: v.value.length ? dayjs(v.value[0]).format('YYYY-MM-DD HH:mm:00') : '',
            visitTimeEnd: v.value.length ? dayjs(v.value[1]).format('YYYY-MM-DD HH:mm:00') : ''
          })
        } else if (v.dataIndex == 'buyTime') {
          return Object.assign(c, {
            buyTimeSta: v.value.length ? dayjs(v.value[0]).format('YYYY-MM-DD HH:mm:00') : '',
            buyTimeEnd: v.value.length ? dayjs(v.value[1]).format('YYYY-MM-DD HH:mm:00') : ''
          })
        } else {
          return Object.assign(c, { [v.dataIndex]: v.value })
        }
      } else {
        return c
      }
    }, {})
    getDispatchList({
      ...obj,
      page: pagination.value.current,
      limit: pagination.value.pageSize,
      serviceState:
        raidoValue.value == 'todo'
          ? obj.serviceState
            ? [obj.serviceState]
            : ['TODO', 'DOING']
          : ['DONE'],
      isSet: true,
      manager: store.userApp.userInfo.id,
      chose: false
    })
      .then((res) => {
        dispatchAllCounts.value = res.data
        List.value = res.data.page.list
        pagination.value.total = res.data.page.total
        loading.value = false
      })
      .catch(() => {
        loading.value = false
      })
  } else {
    obj = conditionEnterprise.value.reduce((c, v) => {
      if (v.value || v.value === 0 || v.value === false) {
        if (v.dataIndex == 'visitTime') {
          return Object.assign(c, {
            visitTimeSta: v.value.length ? dayjs(v.value[0]).format('YYYY-MM-DD HH:mm:00') : '',
            visitTimeEnd: v.value.length ? dayjs(v.value[1]).format('YYYY-MM-DD HH:mm:00') : ''
          })
        } else if (v.dataIndex == 'buyTime') {
          return Object.assign(c, {
            begin: v.value.length ? dayjs(v.value[0]).format('YYYY-MM-DD HH:mm:00') : '',
            end: v.value.length ? dayjs(v.value[1]).format('YYYY-MM-DD HH:mm:00') : ''
          })
        } else {
          return Object.assign(c, { [v.dataIndex]: v.value })
        }
      } else {
        return c
      }
    }, {})
    managerPage({
      ...obj,
      workOrderState: raidoValue.value == 'todo' ? '待派单' : '已派单',
      serviceState: obj.serviceState ? [obj.serviceState] : [],
      page: pagination.value.current,
      limit: pagination.value.pageSize,
      isManager: zhuguanShow.value,
      isLeader: zuzhangShow.value,
      isPeTodo: true
      //   chose: true
    })
      .then((res) => {
        dispatchAllCounts.value = res.data
        List.value = res.data.page.list
        pagination.value.total = res.data.page.total
        loading.value = false
      })
      .catch(() => {
        loading.value = false
      })
  }
}
onMounted(() => {
  if (store.userApp.permissionList.indexOf('eprofile:pe:zhuguan') != -1) {
    zhuguanShow.value = true
  } else {
    zhuguanShow.value = false
  }
  if (store.userApp.permissionList.indexOf('eprofile:pe:zuzhang') != -1) {
    zuzhangShow.value = true
  } else {
    zuzhangShow.value = false
  }
  if (route.query && route.query.type === 'manaPlan') {
    showDetail.value = true
    nextTick(() => {
      manaPlanRef.value.init({
        ...route.query,
        empi: route.query.empi,
        userId: route.query.userId,
        back: route.query.back,
        backtype: route.query.backtype ? route.query.backtype : ''
      })
    })
  }
  enterpriseShowThree.value = true
  condition.value = [...condition.value, ...conditioning.value]
  columns.value = [...columns.value, ...columnsTodo.value]
  loading.value = true
  getDataList()
})

const manaPlanRef = ref()
const addOrUpdateHandle = (data) => {
  showDetail.value = true
  nextTick(() => {
    manaPlanRef.value.init(data)
  })
}
// 个人和企业切换
const tabEnterpriseDataCli = (item, index) => {
  conditionEnterprise.value = []
  condition.value = []
  columns.value = []
  if (index == 0) {
    enterpriseShow.value = true
    enterpriseShowTwo.value = false
    enterpriseShowThree.value = false
    if (raidoValue.value == 'todo') {
      enterpriseShowTwo.value = true
      enterpriseShowThree.value = true
      columns.value = [...columns.value, ...columnsTodo.value]
      condition.value = [...condition.value, ...conditioning.value]
    } else {
      columns.value = [...columns.value, ...columnsDone.value]
      condition.value = [...condition.value, ...conditionTodo.value]
    }
  } else {
    enterpriseShow.value = false
    enterpriseShowTwo.value = false
    enterpriseShowThree.value = false
    if (raidoValue.value == 'todo') {
      columnsTodoTwo.value[7].title = '待执行员工'
      columnsTodoTwo.value[7].dataIndex = 'todoNum'
      columnsTodoTwo.value[7].key = 'todoNum'
      columns.value = [...columns.value, ...columnsTodoTwo.value]

      conditionEnterprise.value = [...conditionEnterprise.value, ...conditionTodoTwo.value]
    } else {
      columnsTodoTwo.value[7].title = '已推送员工'
      columnsTodoTwo.value[7].dataIndex = 'doneNum'
      columnsTodoTwo.value[7].key = 'doneNum'
      columns.value = [...columns.value, ...columnsTodoTwo.value]
      conditionEnterprise.value = [...conditionEnterprise.value, ...conditionTodoTwo.value]
    }
  }
  tabEnterpriseDataActive.value = index
  reset()
}
// 待办列表 多选
const rowSelection = computed(() => {
  if (raidoValue.value === 'todo') {
    return {
      selectedRowKeys: unref(selectedKeys),
      onChange: onSelectChange,
      getCheckboxProps: (record) => ({
        disabled: !record.questionState // 禁用
      })
    }
  } else {
    return null
  }
})

// 选择
const onSelectChange = (selectedRowKeys: Key[]) => {
  selectedKeys.value = selectedRowKeys
}

// 患者详情
const patientInfoRef = ref()
const toDetail = (data) => {
  showPatientInfo.value = true
  nextTick(() => {
    patientInfoRef.value.init(data)
  })
}

// 批量推送
const batchPush = () => {
  if (selectedKeys.value.length > 0) {
    isPushPlan.value = true
  } else {
    message.warning('请选择需要推送方案的患者')
  }
}

const raidoValueChange = (e) => {
  selectedKeys.value = []
  conditionEnterprise.value = []
  condition.value = []
  columns.value = []
  console.log(e.target.value)
  if (tabEnterpriseDataActive.value == 0) {
    enterpriseShow.value = true
    enterpriseShowTwo.value = false
    enterpriseShowThree.value = false
    if (raidoValue.value == 'todo') {
      enterpriseShowTwo.value = true
      enterpriseShowThree.value = true
      columns.value = [...columns.value, ...columnsTodo.value]
      condition.value = [...condition.value, ...conditioning.value]
    } else {
      columns.value = [...columns.value, ...columnsDone.value]
      condition.value = [...condition.value, ...conditionTodo.value]
    }
  } else {
    enterpriseShow.value = false
    enterpriseShowTwo.value = false
    enterpriseShowThree.value = false
    if (raidoValue.value == 'todo') {
      columnsTodoTwo.value[7].title = '待执行员工'
      columnsTodoTwo.value[7].dataIndex = 'todoNum'
      columnsTodoTwo.value[7].key = 'todoNum'
      columns.value = [...columns.value, ...columnsTodoTwo.value]
      conditionEnterprise.value = [...conditionEnterprise.value, ...conditionTodoTwo.value]
    } else {
      columnsTodoTwo.value[7].title = '已推送员工'
      columnsTodoTwo.value[7].dataIndex = 'doneNum'
      columnsTodoTwo.value[7].key = 'doneNum'
      columns.value = [...columns.value, ...columnsTodoTwo.value]
      conditionEnterprise.value = [...conditionEnterprise.value, ...conditionTodoTwo.value]
    }
  }

  reset()
}

const searchHandle = () => {
  pagination.value.current = 1
  loading.value = true
  getDataList()
}
const reset = () => {
  pagination.value.current = 1
  pagination.value.pageSize = 10
  condition.value.map((v) => {
    if (v.type === 'select') {
      v.value = undefined
    } else if (v.type === 'dayRange') {
      v.value = []
    } else {
      v.value = ''
    }
  })
  conditionEnterprise.value.map((v) => {
    if (v.type === 'select') {
      v.value = undefined
    } else if (v.type === 'dayRange') {
      v.value = []
    } else {
      v.value = ''
    }
  })
  selectedKeys.value = []
  loading.value = true
  getDataList()
}
// 查看员工
const patientInfoRefTwo = ref()
const toEnterprise = (data) => {
  console.log(raidoValue.value)
  showPatientInfoTwo.value = true
  nextTick(() => {
    patientInfoRefTwo.value.initRouterInfo(data, raidoValue.value, tabEnterpriseDataActive.value)
  })
}
const gopatientBackTwo = () => {
  showPatientInfoTwo.value = false
  loading.value = true
  getDataList()
}
const goBack = () => {
  router.replace({ query: {} })
  showDetail.value = false
  showPatientInfo.value = false
  loading.value = true
  getDataList()
}

const gopatientBack = () => {
  showDetail.value = false
  showPatientInfo.value = false
  loading.value = true
  getDataList()
}

// 确认推送
const pushPlanBtn = () => {
  const selectData = List.value.filter((v) => {
    if (selectedKeys.value.includes(v.id)) {
      return v
    }
    return false
  })
  isCreateHealthInfo.value = true
  console.log(selectData)
  batchPushScheme(selectData)
    .then((res) => {
      message.success('推送成功')
      isPushPlan.value = false
      selectedKeys.value = []
      loading.value = true
      getDataList()
    })
    .catch(() => {
      isCreateHealthInfo.value = false
    })
}

const followInfoRef = ref()
const questionAnswerForm = (data) => {
  const params = {
    questionnaireRecordId: data.questionnaireRecordId,
    empi: data.empi
  }

  followInfoRef.value.init(params)
}
</script>
<style lang="less" scoped>
.header {
  display: flex;
  justify-content: space-between;

  .tabEnterprise {
    position: relative;
    margin-bottom: 16px;

    p {
      display: inline-block;
      width: 60px;
      height: 32px;
      margin: 0;
      margin-right: 4px;
      font-size: 14px;
      line-height: 32px;
      color: #4e5969;
      text-align: center;
      vertical-align: middle;
      background: #f7f8fa;
      border: 1px solid #e5e6eb;
      border-top-left-radius: 2px;
      border-top-right-radius: 2px;

      &.on {
        position: relative;
        z-index: 1;
        font-weight: 700;
        color: #165dff;
        background: #fff;
        border-bottom: 0;
      }

      &:hover {
        cursor: pointer;
        opacity: 0.8;
      }
    }

    &::after {
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      content: '';
      border-bottom: 1px solid #e5e6eb;
    }
  }
}

.main {
  flex: 1 1 auto;
  margin-top: 16px;
  overflow: hidden auto;

  .patName {
    color: #1677ff;

    &:hover {
      cursor: pointer;
      opacity: 0.7;
    }
  }

  .peOrgType {
    padding: 2px 7px;
    font-size: 12px;
    color: #f7ba1e;
    background: #fffce8;
    border: 1px solid #f7ba1e;
    border-radius: 2px;

    &.peOrgType0 {
      color: #00b42a;
      background: #e8ffea;
      border-color: #00b42a;
    }

    &.peOrgType1 {
      color: #3491fa;
      background: #e8f7ff;
      border-color: #3491fa;
    }

    &.peOrgType2 {
      color: #0fc6c2;
      background: #e8fffb;
      border-color: #0fc6c2;
    }
  }
}

.page-content {
  padding: 16px;
}
</style>
